package com.taplinker.client.im.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.taplinker.client.R;
import com.taplinker.client.im.domain.EnumMessageType;
import com.taplinker.client.im.protocol.GetVisitorQueResponse;
import com.taplinker.client.im.util.SmileyParser;

public class HistoryAdapter extends BaseAdapter {
	private Context context;
	private SmileyParser parser;
	private List<GetVisitorQueResponse> conversations = new ArrayList<GetVisitorQueResponse>();

	public List<GetVisitorQueResponse> getConversations() {
		return conversations;
	}

	public void setConversations(List<GetVisitorQueResponse> conversations) {
		this.conversations = conversations;
	}

	private GetVisitorQueResponse currentConversation;

	public HistoryAdapter() {
		super();
	}

	public HistoryAdapter(Context context, List<GetVisitorQueResponse> conversations) {
		this.context = context;
		this.conversations = conversations;
	}

	public void clear() {
		conversations.clear();
	}

	@Override
	public int getCount() {
		return conversations.size();
	}

	@Override
	public Object getItem(int position) {
		return conversations.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.message_history_item, null);

			holder = new ViewHolder();
			holder.servicerName = (TextView) convertView.findViewById(R.id.servicer_name);
			holder.unreadCount = (TextView) convertView.findViewById(R.id.unread_count_tv);
			holder.appName = (TextView) convertView.findViewById(R.id.app_name_tv);
			holder.lastMsgSummary = (TextView) convertView.findViewById(R.id.last_msg_summary_tv);
			holder.lastMsgTime = (TextView) convertView.findViewById(R.id.last_meg_time_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		currentConversation = conversations.get(position);
		if (currentConversation == null) {
			return null;
		}

		holder.servicerName.setText(currentConversation.getCsname());
		holder.appName.setText(currentConversation.getAppName());
		holder.lastMsgTime.setText(SimpleDateFormat.getInstance().format(currentConversation.getUpdateDate()));

		if (currentConversation.getMessageType() != null) {
			if (EnumMessageType.IMAGE.getType().equalsIgnoreCase(currentConversation.getMessageType())) {
				holder.lastMsgSummary.setText("[图片]");
			} else if (EnumMessageType.AUDIO.getType().equalsIgnoreCase(currentConversation.getMessageType())) {
				holder.lastMsgSummary.setText("[语音]");
			}else {
				parser = new SmileyParser(context);
				holder.lastMsgSummary.setText(parser.replaceText(currentConversation.getLastMessage().toString(), "[表情]"));
			}
		}

		if (currentConversation.getUnReadCount() != 0) {
			holder.unreadCount.setVisibility(View.VISIBLE);
			holder.unreadCount.setText(""+ currentConversation.getUnReadCount());
		} else {
			holder.unreadCount.setVisibility(View.GONE);
		}

		return convertView;
	}

	class ViewHolder {
		TextView servicerName;
		TextView unreadCount;
		TextView appName;
		TextView lastMsgSummary;
		TextView lastMsgTime;
	}

}